package com.leyou.item.controller;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.TbSpecGroup;
import com.leyou.item.service.SpecService;
import com.sun.net.httpserver.HttpsConfigurator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理规格组 和 规格参数
 */
@RestController
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 根据分类id查询分组集合
     * @param cid
     * @return
     */
    @GetMapping("/spec/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> findSpecGroupListByCid(@RequestParam(name = "id")Long cid){
        return ResponseEntity.ok(specService.findSpecGroupListByCid(cid));
    }

    /**
     * 新增规格组
     * @param tbSpecGroup
     * @return
     */
    @PostMapping("/spec/group")
    public  ResponseEntity<Void> saveSpecGroup(@RequestBody  TbSpecGroup tbSpecGroup){
        specService.saveSpecGroup(tbSpecGroup);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据条件查询规格参数集合
     * @param cid
     * @param gid
     * @param searching
     * @return
     */
    @GetMapping("/spec/params")
    public ResponseEntity<List<SpecParamDTO>> findSpecParamListByCondition(@RequestParam(name = "cid",required = false)Long cid,
                                                                           @RequestParam(name = "gid",required = false)Long gid,
                                                                           @RequestParam(name = "searching",required = false)Boolean searching){
        return ResponseEntity.ok(specService.findSpecParamListByCondition(cid,gid,searching));
    }

    /**
     * 新增规格参数
     * @param specParamDTO
     * @return
     */
    @PostMapping("/spec/param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO specParamDTO){
        specService.saveSpecParam(specParamDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据cid 查询 规格组 和 组内参数的集合
     * @param cid
     * @return
     */
    @GetMapping("/spec/of/category")
    public ResponseEntity<List<SpecGroupDTO>> findSpecGroupByCid(@RequestParam(name = "id")Long cid){
        return ResponseEntity.ok(specService.findSpecGroupByCid(cid));
    }
}
